<?php
include_once(_CORE_PATH. "entity_prototype.php");
include_once(_APP_PATH. "business/media/media_entity.php");
class ArticleEntryEntity extends entity_prototype
{ 
	protected $_name = 'article_entry';
	
	public function __construct()
	{
	
	}
	
	public function SetId($id)
	{
		$this->Set('id', (int)$id);		
	}	
	
	public function SetBlogId($id)
	{
		$this->Set('blogId', (int)$id);		
	}	
	
	public function GetBlogId()
	{
		return (int)$this->Get('blogId');		
	}

	public function GetPublishDate($format = 'd.m.Y H:i')
	{
		return date($format, $this->Get('publishDate'));		
	}	
	
	public function SetPublishDate($date)
	{
		$mktime = strtotime($date);
		$this->Set('publishDate', $mktime === false ? time(): $mktime);
	}

	public function GetMediaFile()
	{
		$obj = $this->Get('mediaFileObject');
		if (!($obj instanceof MediaEntity))
		{
			$obj = new MediaEntity();

			$data = $this->GetData();
			
			$mediaData = array();
			foreach ($data as $key => $value)
			{
				$pos = strpos($key, 'media_');
				if (false !== $pos)
				{
					$mediaData[substr($key, 6)] = $value;
				}
			}

			$obj->AddData($mediaData);	
			$this->Set('mediaFileObject', $obj);
		}
		
		return $obj;
	}
	
	public function SetMediaFile(MediaEntity $file)
	{
		$this->Set('mediaFileObject', $file);
	}
	
	public function GetCategories()
	{
		$cats = $this->Get('categories');
		if (!is_array($cats))
		{
			$this->SetCategoriesNorm($cats);
			$cats = $this->Get('categories');
		}
		
		return $cats;
	}

	public function SetCategories(array $cats)
	{
		$cats = array_unique(array_map('intval', $cats));
		$this->Set('categories', $cats);
	}

	public function SetCategoriesNorm($catStr)
	{
		$cats = explode(',', trim((string)$catStr, ','));
		$this->Set('categories', $cats);
	}	
	
	public function GetCategoriesNorm()
	{
		$cats = $this->Get('categories');
		if (is_array($cats))
		{
			$cats = ',' .implode(',', $cats). ',';
		}
		
		return $cats;
	}	
}